package com.appspot.android2gather.server;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.android2gather.activity.ErrorMessage;
import com.appspot.android2gather.persistance.ActivityEntity;
import com.appspot.android2gather.persistance.EMF;
import static com.appspot.android2gather.activity.RequestParameters.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.Gson;
public class AttendanceManager
    extends HttpServlet
{
    /**
     * 
     */
    private static final long serialVersionUID = 4881704098208118915L;

    private static Gson gson = new Gson();

    /**
     * gets the Number of participants for an Activity
     */
    @Override
    protected void doGet( HttpServletRequest req, HttpServletResponse resp )
        throws ServletException, IOException
    {
        if ( assigned( req.getParameter( "m" ) ) )
        {
            if ( req.getParameter( "m" ).equals( "post" ) )
            {
                doPost( req, resp );
                return;
            }
            if ( req.getParameter( "m" ).equals( "put" ) )
            {
                doPut( req, resp );
                return;
            }
            if ( req.getParameter( "m" ).equals( "delete" ) )
            {
                doDelete( req, resp );
                return;
            }
        }
        String id = req.getParameter( ID );

        if ( assigned( id ) )
        {
            EntityManager em = EMF.get().createEntityManager();
            try
            {
                // em.getTransaction().begin();
                ActivityEntity activity = em.find( ActivityEntity.class, Long.parseLong( id ) );
                String activityGson;
                if ( null != activity )
                {
                    activityGson = gson.toJson( activity.getNumberOfAttendees(), Integer.class );
                }
                else
                {
                    activityGson = gson.toJson( activity, ActivityEntity.class );
                }
                resp.getWriter().write( activityGson );
                resp.flushBuffer();

            }
            finally
            {
                // em.getTransaction().commit();
                em.close();
            }
        }
        else
        {
            String errorGson = gson.toJson( new ErrorMessage( "No id specified." ), ErrorMessage.class );
            resp.getWriter().write( errorGson );
            resp.flushBuffer();
        }
    }

    /**
     * subscribes an attendee to an Activity
     */
    @Override
    protected void doPost( HttpServletRequest req, HttpServletResponse resp )
        throws ServletException, IOException
    {
        String id = req.getParameter( ID );

        if ( assigned( id ) )
        {
            addAttendee( req, resp, id );
        }
    }

    /**
     * unregisters an attendee from an Activity.
     */
    @Override
    protected void doDelete( HttpServletRequest req, HttpServletResponse resp )
        throws ServletException, IOException
    {
        String id = req.getParameter( ID );

        if ( assigned( id ) )
        {
            removeAttendee( req, resp, id );
        }
    }

    protected void addAttendee( HttpServletRequest req, HttpServletResponse resp, String id )
        throws IOException
    {
        User user = null; 
        if (assigned(req.getParameter(EMAIL))){
        	user = new User(req.getParameter(EMAIL),"gmail.com");
        }
        else
        {
        	user = UserServiceFactory.getUserService().getCurrentUser();
        }
        EntityManager em = EMF.get().createEntityManager();
        try
        {
            ActivityEntity activity = em.find( ActivityEntity.class, Long.parseLong( id ) );
            String response = "";
            activity.addAttendee( user );

            response = gson.toJson( new Boolean( true ), Boolean.class );
            resp.getWriter().write( response );
            resp.flushBuffer();
        }
        finally
        {
            em.close();

        }
    }

    protected void removeAttendee( HttpServletRequest req, HttpServletResponse resp, String id )
        throws IOException
    {
        User user = UserServiceFactory.getUserService().getCurrentUser();
        EntityManager em = EMF.get().createEntityManager();
        try
        {
            String response = "";
            ActivityEntity activity = em.find( ActivityEntity.class, Long.parseLong( id ) );
            response = gson.toJson( new Boolean( activity.removeAttendee( user ) ), Boolean.class ); // delete
            resp.getWriter().write( response );
            resp.flushBuffer();

        }
        finally
        {
            em.close();
        }
    }
}
